// Open Source: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Project: YoProgramo.NET (http://code.google.com/p/yoprogramo/)
// Autor: Fabian Figueredo (info@fabianfigueredo.com.ar)
// Blog: blog.yoprogramo.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using YoProgramo.Domain.BusinessLogic.Attributes;
using YoProgramo.Domain.BusinessLogic.Service;
using YoProgramo.Domain.Core;
using YoProgramo.Domain.Core.Interfaces;
using YoProgramo.Infrastructure.Tools.PagedList;

namespace YoProgramo.Web.Controllers
{
    public class CategoryController : Controller
    {
        private ICategoryRepository _repository;
        private ICategoryService _service;
        private IStoryService _storyservice;

        public CategoryController(ICategoryRepository repository,
                                ICategoryService service, IStoryService storyservice
                                )
        {
            _repository = repository;
            _service = service;
            _storyservice = storyservice;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult View(string slug, int? page) {
            ICategory category = _service.GetBySlug(slug);
            if (category == null)
                return RedirectToRoute("Default", new { controller = "Home", action = "Index", id = "" });

            ViewData["Title"] = "Category: " + category.Name;
            ViewData["UrlPaging"] = Url.RouteUrl("ViewItemsPaging", new { slug = slug, page = "__PAGENUM__" }); //For paging

            var storyList = (IPagedList<IStory>)_storyservice.GetByCategory(category).ToPagedList(page ?? 0, 20);
            return View(storyList);
        }


        public ActionResult Details(int id)
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        } 


        [HandleError]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateAntiForgeryToken]
        [YpAuthorize(Roles = new Roles[] { Roles.Administrator })]
        public ActionResult Create(FormCollection formValues)
        {
            try
            {
                if (!string.IsNullOrEmpty(formValues["Name"]))
                {
                    _service.Create(formValues["Name"]);
                }
                return RedirectToRoute("Default", new {controller = "Home", action = "Index"});
            }
            catch
            {
                return View();
            }
        }
 
        public ActionResult Edit(int id)
        {
            return View();
        }


        [HandleError]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateAntiForgeryToken]
        [YpAuthorize(Roles = new Roles[] { Roles.Administrator })]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
